package club.dmyang.controller;

import club.dmyang.common.enums.RespCodeEnums;
import club.dmyang.common.vo.ResposeVo;
import club.dmyang.entity.Carousel;
import club.dmyang.entity.Tag;
import club.dmyang.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by brss on 2018/8/17.
 */
@Controller
@RequestMapping("/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    /**
     * 分页列出所有轮播
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResposeVo list(){
        List<Carousel> carouselList = carouselService.findAll();
        return new ResposeVo(0,"成功",carouselList.size(),carouselList);
    }

    /**
     * 跳转到编辑页面（填充响应表单）
     * @param model
     * @return
     */
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model){
        Carousel carousel = carouselService.findOneById(id);
        model.addAttribute("carousel",carousel);
        return "admin/carousel-add";
    }

    /**
     * 更新一个轮播
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResposeVo update(Carousel carousel){
        if (carouselService.updateOneById(carousel)){
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "更新成功");
        }else {
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "更新失败");
        }

    }

    /**
     * 删除一个轮播
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete/{id}")
    public ResposeVo delete(@PathVariable("id") Integer id){
        if (carouselService.delOneById(id)) {
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "删除成功");
        }else {
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "删除失败");
        }
    }

    @ResponseBody
    @RequestMapping("/add")
    public ResposeVo add(Carousel carousel){
        if (carouselService.addOne(carousel)) {
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "添加成功");
        }else {
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "添加失败");
        }
    }

}
